<template>
    <div>
        <el-table :data="pager.lists" border @selection-change="handleSelectionChange">
                 
                 <el-table-column label="品名" prop="name" show-overflow-tooltip />
                 <el-table-column label="规格" prop="guige" show-overflow-tooltip />
                 <el-table-column label="下料数量" prop="num" show-overflow-tooltip />
                 <el-table-column label="操作人" prop="create" show-overflow-tooltip />
                 <el-table-column label="时间" prop="create_time" show-overflow-tooltip />
                 <el-table-column label="操作" width="120" fixed="right">
                     <template #default="{ row }">
                         <el-button
                             v-perms="['orders.orders_xialiao/delete']"
                             type="danger"
                             link
                             @click="handleDelete(row)"
                         >
                             删除
                         </el-button>
                     </template>
                 </el-table-column>
             </el-table>
       
        <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        <edit-popup v-if="showEdit" ref="editRef" :dict-data="dictData" @success="getLists" @close="showEdit = false" />
    </div>
</template>

<script lang="ts" setup name="ordersXialiaoLists">
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import { apiOrdersXialiaoLists, apiOrdersXialiaoDelete } from '@/api/orders/orders_xialiao'
import { timeFormat } from '@/utils/util'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
const route = useRoute()
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
// 是否显示编辑框
const showEdit = ref(false)


// 查询条件
const queryParams = reactive({
    order_id: '',
    detail_id:  route.query.detail_id
  
})

// 选中数据
const selectData = ref<any[]>([])

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
    selectData.value = val.map(({ id }) => id)
}

// 获取字典数据
const { dictData } = useDictData('')


// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiOrdersXialiaoLists,
    params: queryParams
})
const setFormData = async (row: Record<any, any>)=>{
    if(row.todo){
        queryParams.order_id=row.id
    }else{
        queryParams.detail_id=row.id
    }
 
    getLists()
}
// 添加
const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

// 编辑
const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.setFormData(data)
}

// 删除
const handleDelete = async (data: any ) => {
    await feedback.confirm('确定要删除？')
    await apiOrdersXialiaoDelete( {id:data.id,num:data.num,order_id:data.order_id} )
    getLists()
}

//getLists()
defineExpose({
    setFormData
})
</script>

